﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace 字典与值类型
{


    public partial class Form1 : Form
    {
        public enum ST
        {
            Free = 0,
            AT,
            AB,
        }

        public class MyClass
        {
            public int value = 0;
        }

        /// <summary>
        /// 以值(int)或值引用为值(int?)，是深复制，原变量不再受这里的影响
        /// </summary>
        Dictionary<string, int> dic = new Dictionary<string, int>();
        /// <summary>
        /// 以枚举为值，是深复制，原变量不再受这里的影响
        /// </summary>
        Dictionary<string, ST> dicST = new Dictionary<string, ST>();
        /// <summary>
        /// 以对象为值，是㳀复制，原对象受这里的影响，这里的值变，原对象的值也跟着变
        /// </summary>
        Dictionary<string, MyClass> dicMyClass = new Dictionary<string, MyClass>();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dic["1"] = 2;
            dicST["1"] = ST.Free;
            dicMyClass["1"].value = 3; 
            label2.Text = dic["1"].ToString();
            label4.Text = dicST["1"].ToString();
            label6.Text = dicMyClass["1"].value.ToString();
        }

        private void MyThread()
        {
            int value = 1;
            ST st = ST.AB;
            MyClass mc = new MyClass();
            mc.value = 1;

            dic.Add("1", value);
            dicST.Add("1", st);
            dicMyClass.Add("1", mc);

            while (true)
            {
                try
                {
                    //value = dic["1"];
                    //在按键上修改值后，下面的值只有以类为值时，才会跟着改变
                    this.Invoke(new EventHandler(delegate {
                        label1.Text = value.ToString();
                        label3.Text = st.ToString();
                        label5.Text = mc.value.ToString();
                    }));
                }
                catch { }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Thread th = new Thread(MyThread);
            th.IsBackground = true;
            th.Start();
        }
    }
}
